<?php
/*
 * Login controller
 */
class LoginController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $ip;
	
	public function init() {
		/* Initialize action controller here */
		$this->controller = $this->getRequest ()->getControllerName ();
		$this->action = $this->getRequest ()->getActionName ();
		
		$this->commonTemplates ();
		
		$this->_smarty->assign ( 'controller', $this->controller );
		$this->_smarty->assign ( 'action', $this->action );
		
		$this->ip = MyClass_Common::getIP ();
	}
	
	public function indexAction() {
		try {
			// action body
			$this->signout ();
			//$this->_smarty->assign ( 'bloguser', PROJECT_ROOT );
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
			
		//print_r($this->_smarty);
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function opAction() {
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'Parameter or argument errors!' );
			}
			//Filter
			//Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter = new Zend_Filter_StripTags ( );
			$username = trim ( $filter->filter ( $this->getParam ( 'username' ) ) );
			$password = trim ( $filter->filter ( $this->getParam ( 'password' ) ) );
			
			if (empty ( $username )) {
				throw new Exception ( 'Please enter a username!' );
			}
			
			if (empty ( $password )) {
				throw new Exception ( 'Please enter a password!' );
			}
			
			// 创建一个 in-memory Mysql 数据库连接
			$this->getDb ( 'osmdb' );
			$authAdapter = new Zend_Auth_Adapter_DbTable ( $this->_dbAdapter, 'arafmi_users', 'username', 'password' );

			// 设置输入的证书的值 (例如，从登陆的表单)
			$authAdapter->setIdentity ( $username )->setCredential ( md5 ( $password ) );
			
			// 执行认证查询，并保存结果
			$result = $authAdapter->authenticate ();
			
			if ($result->isValid ()) {
				
				$result_check = $authAdapter->getResultRowObject ();

				if ($result_check->isdisabled )
					throw new Exception ( 'The user have been deleted！' );
					
				if(!$result_check->iscanlogin)	
						throw new Exception ( '该用户被限制登陆！' );
				//if (!$result_check->isadmin)
				//	throw new Exception ( '该用户不是管理员，不能登陆！' );
				
				$auth = Zend_Auth::getInstance ();
				$auth->getStorage ()->write ( $authAdapter->getResultRowObject ( array ('uid', 'username', 'name','branch_id','isadmin' ) ) );
				
				$sql = 'update arafmi_users set login_num=login_num+1, last_visit_time='.time().', last_visit_ip ="' . $this->ip . '" where uid=' . $auth->getStorage ()->read ()->uid;
				$this->_dbAdapter->query ( $sql );

//print_r($authAdapter);exit;
				// 跳转
				//$this->feedback ( '登录成功！', 'Caution', '/' );
				$this->_redirect ( '/' );
			
			} else {
				// 获取错误信息
				$msg = '';
				foreach ( $result->getMessages () as $message ) {
					$msg .= "$message\n";
					
				//$this->feedback ( '登录失败！' . $msg, 'Caution', '/', 'warning' );
				$this->feedback ( 'User name or password is not correct!' , 'Caution', '/', 'warning' );
				
				}
				
			// 显示错误信息到模板
			//$this->view->msg = $msg;
			}
		
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', '/', 'warning' );
		}
	}
	
	private function signout() {
		try {
			$auth = new Zend_Session_Namespace ( 'auth' );
			$auth->setExpirationSeconds ( 1 );
			Zend_Auth::getInstance ()->clearIdentity ();
			
			return true;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', '/', 'warning' );
		}
	}
	
	public function outAction() {
		try {
			
			$this->signout ();
			
			$this->feedback ( 'Logout Successfully!', 'Caution', '/', 'tip' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	/**
	 * messageAction 
	 * 
	 * @access public
	 * @return void
	 */
	public function messageAction() {
		//javascript:window.history.back();
		$url = $this->getParam ( 'url' );
		
		if (substr ( $url, 0, 10 ) == 'javascript'){
			$urltype = 1;
			$url=substr($url,11,strlen($url));	
		}else
			$urltype = 0;
			
		$this->_smarty->assign ( 'url', $url );
		$this->_smarty->assign ( 'urltype', $urltype );
		$this->_smarty->assign ( 'message', $this->getParam ( 'message', 'Unknown notice accrued.' ) );
		$this->_smarty->assign ( 'title', $this->getParam ( 'title', 'Notice' ) );
		$this->_smarty->assign ( 'icon', $this->getParam ( 'icon', 'note' ) );
		$this->_smarty->assign ( 'delay', $this->getParam ( 'delay', 3 ) );
		
		$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
	}
	
	//reserve statement as below
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}
}